package com.cn.yunhe.rentcars.web.permission;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.mapper.permission.AccountMapper;
import com.cn.yunhe.rentcars.mapper.permission.RoleMapper;
import com.cn.yunhe.rentcars.model.permission.Account;
import com.cn.yunhe.rentcars.model.permission.Role;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 权限管理--用户管理
 * @author 刘源
 *
 */

@RestController
public class AccountController {

	@Autowired
	private AccountMapper accountMapper;
	
	@Autowired
	private RoleMapper roleMapper;
	
	/**
	 * @return  用户信息展示页面
	 */
	@RequestMapping(value="/accounts", method=RequestMethod.GET)
	public ModelAndView showAccountsInfo() {	
		return new ModelAndView("account-list");
	}
	
	@RequestMapping(value="/sysAccounts", method=RequestMethod.GET)
	public ModelAndView showSysAccountInfo(HttpServletRequest request, HttpServletResponse response){
		String userName = (String) request.getSession().getAttribute("username");
		Account account = accountMapper.getAccountByName(userName);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("account", account);
		return new ModelAndView("system-account",map);
	}
	
	/**
	 * @return  添加页面
	 */
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public ModelAndView showAddAdmin() {	
		List<Role> roleList = roleMapper.getIsabledRoles();
		Map<String,Object> modelMap = new HashMap<String,Object>();
		modelMap.put("roleList", roleList);
		return new ModelAndView("account-add",modelMap);
	}
	
	/**
	 * 点击编辑按钮显示编辑页面，用户信息默认显示出来
	 * @param uid
	 * @return 编辑页面
	 */
	@RequestMapping(value="/account-edit", method=RequestMethod.GET)
	public ModelAndView showEditAdmin(@RequestParam String uid) {	
		
		Account account = accountMapper.getAccountById(uid);
		
		Map<String,Object> modelMap = new HashMap<String,Object>();
		modelMap.put("account", account);
		List<Role> roleList = roleMapper.getIsabledRoles();
		modelMap.put("roleList", roleList);
		return new ModelAndView("account-edit",modelMap);		
	}
	
	
	
	/**
	 * 添加新用户
	 * 
	 * @param account
	 */
	@RequestMapping(value="/addAccount", method=RequestMethod.POST)
	public void addAccount(@RequestBody Account account){
		account.setUid(UUID.randomUUID().toString());
		accountMapper.addAccount(account);
	}
	
	/**
	 * 删除单个账户信息
	 * @param uid
	 */
	@RequestMapping(value="/delAccount", method=RequestMethod.POST)
	public void delAccount(@RequestParam String uid){
		accountMapper.delAccount(uid);
	}
	
	/**
	 * 批量删除账户信息，传递参数用post
	 * 
	 * @param uid
	 */
	@RequestMapping(value="/delAccounts", method=RequestMethod.POST)
	public void delAccounts(@RequestParam String uids){
		List<String> uidList = Arrays.asList(uids.split(","));  		
		accountMapper.delAccounts(uidList);
	}
	
	
	
	/**
	 *  修改账户信息
	 * @param uid
	 * @param account
	 */
	@RequestMapping(value="/editAccount/{uid}", method=RequestMethod.PUT)
	public void editAccount(@PathVariable("uid") String uid,@RequestBody Account account){
		account.setUid(uid);
		accountMapper.editAccount(account);
	}
	
	
	/**
	 * 搜索查询用户列表信息
	 * 
	 * @param username
	 * @param draw 
	 * @param start
	 * @param length
	 * @return
	 */
	@RequestMapping(value="/account/conditions", method=RequestMethod.GET)
	public Map<String,Object> getLikeAccounts(@RequestParam String datemin,@RequestParam String datemax,@RequestParam String username,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		  
		  Map<String,String> params =  new HashMap<String,String>();
		  params.put("username", username);
		  params.put("datemin", datemin);
		  params.put("datemax", datemax);
		
		  int pageNum = (start/length)+1;
		  int pageSize = length;		 
		  
	      PageHelper.startPage(pageNum, pageSize);
	      
	      List<Account> accountList = accountMapper.searchAccounts(params);
	      for(Account account:accountList){
	    	  Role role = roleMapper.queryByRoleId(account.getRoleCode());
	    	  account.setRoleName(role.getRoleName());
	      }
	      PageInfo<Account> page = new PageInfo<Account>(accountList);
	      Map<String,Object> rsMap = new HashMap<String,Object>();
	      rsMap.put("draw",draw);
	      rsMap.put("data", page.getList());	      
	      rsMap.put("recordsTotal", page.getTotal());
	      rsMap.put("recordsFiltered", page.getTotal());
	      return rsMap;
	}
	
	/**
	 * 编辑用户状态
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/setUserState", method=RequestMethod.POST)
	public void handleUserState(@RequestParam String uid,@RequestParam  int userState){ 
		Account account = new Account();
		account.setUid(uid);
		account.setUserState(userState);
		accountMapper.setUserState(account);
	}
}
